package com.teamwork.module03.controller;

import com.teamwork.common.entity.common.User;
import com.teamwork.module03.service.UserService;
import com.teamwork.common.entity.common.requestentity.ReqUserBody;
import com.teamwork.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/page")
    public Result pageSelect(@RequestBody ReqUserBody reqUserBody){
        return userService.pageSelect(reqUserBody);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        return userService.deleteById(id);
    }

    @PostMapping("/delete/buch")
    public Result deleteBuch(@RequestBody Map<String,Integer[]> map){
        Integer[] ids = map.get("ids");
        return userService.deleteBuchByIds(ids);
    }
    @GetMapping("/getmine")
    public Result getMineByUid(Integer userid){
        return userService.getMineByUid(userid);
    }

    @GetMapping("/getuserinfo")
    public Result getUserInfo(Integer userid){
        return userService.getUserInfo(userid);
    }

    //修改用户信息
    @PostMapping("/edituserinfo")
    public Result editUserInfo(@RequestBody User user){
        //log.info("用户：{}",user);
        return userService.editUserInfo(user);
    }
    //管理员修改用户信息
    @PostMapping("/update/{userid}")
    public Result updateUser(@PathVariable("userid")Integer userid, MultipartFile img,
                             @Param("username")String username,@Param("nickname")String nickname,
                             @Param("email")String email,@Param("phone")String phone,
                             @Param("password")String password) throws IOException {

        return userService.updateUser(userid,img,username,password,nickname,email,phone);

    }
    //管理员新增用户
    @PostMapping("/insert/{userid}")
    public Result insertUser(@PathVariable("userid")Integer userid, MultipartFile img,
                             @Param("username")String username,@Param("nickname")String nickname,
                             @Param("email")String email,@Param("phone")String phone,
                             @Param("password")String password,@Param("role")Integer role) throws IOException {
       return userService.insertUser(userid,img,username,nickname,password,email,phone,role);


    }


}
